from django.db import models


# Create your models here.
# 重点 数据库表结构  先设计再 数据迁移到数据库

class TestCaseModel(models.Model):
    name = models.CharField(verbose_name='用户名',max_length=32)
    password = models.CharField(verbose_name='密码', max_length=64, null=True)
    age = models.IntegerField(verbose_name='年龄', null=False, default=20)
    sex=models.IntegerField(verbose_name='性别', null=True, blank=True,default=1)
    class Meta:
        db_table = 'test_case2'
        verbose_name = '用户信息表'
        verbose_name_plural='用户信息表'
        ordering = ('id',)


class UserInfo(models.Model):
    name = models.CharField(verbose_name='用户名', max_length=32)
    password = models.CharField(verbose_name='密码', max_length=64)
    age = models.CharField(verbose_name='年龄', max_length=20, null=False,blank=True)

    class Meta:
        db_table = 'userinfo'
        # verbose_name = '用户信息表'
        # verbose_name_plural = '用户信息表'


class Author(models.Model):
    author_name = models.CharField(max_length=32, null=False)


class AuthorDetail(models.Model):
  author_sex = models.IntegerField(choices=((0, "男"), (1, "女")), null=False)
  author_phone = models.CharField(max_length=11, null=False)
  author_age = models.IntegerField(null=False,default=30)
# 一对一关系的两个类，关系模型写到哪个类里都行，要顾及代码执行顺序，要写到下面的类里
# models.CASCADE是级联删除，即关系方数据删掉，本表的相关信息也一并删掉
  author =models.OneToOneField(to=Author, to_field="id", on_delete=models.CASCADE)


#出版社
class Publisher(models.Model):
   publisher_name = models.CharField(max_length=30,null=False)
   publisher_address = models.CharField(max_length=50,null=False)
   publisher_city = models.CharField(max_length=30,null=False)
   publisher_website = models.URLField(null=True)

class Book(models.Model):
   book_name = models.CharField(max_length=64,null=False)
#多对多关系与一对一关系同理，写到哪个都行，也是顾及代码执行顺序，要写到下面的类里
   author = models.ManyToManyField(to=Author)
#一对多关系，要写到多的类里
   publisher =models.ForeignKey(to=Publisher,to_field="id",on_delete=models.CASCADE,null=True,blank=True)
   price = models.FloatField(max_length=6,null=None)